define(["jquery","bbcare"],function($,$bb){
    var URL = $bb.baseUrl("nutrition");
	var servers = {};

    /**
     * 获取上一个页面传递过来的参数
     */
    servers.getPageParams = function(){
        return $bb.getPageParams();
    }

	/**
     * 查询食品库
     */
	servers.ajaxFoodlibrary = function(callback){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/foodlibrary/list",
            success: function(data){
                if(data.success){
                    callback(data.rows);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "查询食品库出错！");
                }
            }
        });
    }

    /**
     * 处理食品库list数据，当前食品库selected
     */
    servers.handleFoodLibirayTab = function(options){
        for (var i = 0; i < options.list.length; i++) {
            options.list[i].selected = options.list[i].foodLibraryId == options.foodLibraryId ? true : false;
        }
        return options;
    }

    /**
     * 查询食物库下食物树形菜单
     */
    servers.ajaxFoodMenus = function(libraryId,callback,req){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/foodtype/menulist",
            data: JSON.stringify({
                data: { 
                    "foodLibraryId":libraryId 
                }
            }),
            success: function (data) {
                if(data.success){
                    callback(data.data,req);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "查询食品库菜单出错！");
                }
            }
        });
    };

    /**
     * 获取nodeId,根据foodId
     */
    servers.getNodeId = function(foodId,data){
        for (var i = 0; i < data.length; i++) {
            if( data[i].refId == foodId ){
                return data[i].id;
            } 
        }
    }

    /**
     * 查询食物营养成分字典列表
     */
    servers.ajaxNutrientDict = function(callback){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/nutrientDictQuery",
            success: function (data) {
                if(data.success){
                    callback(data.data);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "查询营养成分字典出错！");
                }
            }
        });
    };

    /**
     * 添加/修改食物分类
     */
    servers.ajaxFoodTypeModify = function(params,callback,className){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/foodTypeModify",
            data: JSON.stringify({
                data: params
            }),
            success: function (data) {
                if(data.success){
                    callback(data.data,className,params.foodLibraryId);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "添加/修改食物分类出错！");
                }
            }
        });
    }

    /**
     * 删除食物分类
     */
    servers.ajaxFoodTypeDel = function(params,callback){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/foodTypeDel",
            data: JSON.stringify({
                data: params
            }),
            success: function (data) {
                if(data.success){
                    callback(data.data);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "删除食物分类出错！");
                }
            }
        });
    };

    /**
     * 获取添加食物的参数
     */
    servers.getSaveFoodParams = function(){
        var foodLibraryId = $("#foodLibirayTab select").val();
        var foodName = $("#foodName").val();
        var foodId = $("#foodName").attr("foodId");
        var foodUnit = $("#foodUnit").val();
        var foodContent = $("#foodContent").val();
        var foodTypeId = $("#foodTypeId").attr("foodTypeId");
        var nutrientsArr = [];
        if($("#nutrientTable tbody tr.original-tr").length > 0){
            $("#nutrientTable tbody tr.original-tr").each(function(i){
                var obj = {};
                obj.nutrientsCode = $(this).attr("nutrientsCode");
                obj.nutrientsId = $(this).attr("nutrientsId");
                obj.nutrientsValue = $(this).attr("nutrientsValue");
                nutrientsArr.push(obj);
            });
        }
        if( $("#nutrientTable tbody tr.new-add-tr").length > 0 ){
            $("#nutrientTable tbody tr.new-add-tr").each(function(i){
                var obj = {};
                obj.nutrientsCode =$(this).attr("nutrientsCode"); //$(this).find("select > option:selected").attr("nutrientsCode");;
                obj.nutrientsId = $(this).attr("nutrientsId");//$(this).find("select").val();
                obj.nutrientsValue = $(this).find("input").val();
                nutrientsArr.push(obj);
            });
        }
        var data = {
            "foodId":foodId,
            "foodLibraryId":foodLibraryId,
            "foodName":foodName,
            "foodUnit":foodUnit,
            "foodContent":foodContent,
            "foodTypeId":foodTypeId,
            "nutrients":nutrientsArr 
        }
        return data;
    }

    /**
     * 添加、修改食物
     */
    servers.ajaxFoodModify = function(params,callback){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/foodModify",
            data: JSON.stringify({
                data: params
            }),
            success: function (data) {
                if(data.success){
                    callback(data.data);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "添加、修改食物出错！");
                }
            }
        });
    }

    /**
     * 删除食物
     */
    servers.ajaxFoodDel = function(params){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/foodDel",
            data: JSON.stringify({
                data: {
                    "foodId":params.foodId
                }
            }),
            success: function (data) {
                if(data.success){
                    params.callBack(data.data);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "删除食物出错！");
                }
            }
        });
    }

    /**
     * 查询食物基本详情
     */
    servers.ajaxFoodDetail = function(foodId,callback){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/food/detail",
            data: JSON.stringify({
                data: {
                    "foodId":foodId
                }
            }),
            success: function (data) {
                if(data.success){
                    callback(data.data);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "查询食物基本详情出错！");
                }
            }
        });
    } 

    /**
     * 删除食物库
     */
    servers.ajaxFoodlibraryDel = function(foodLibraryId,callback){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition//foodlibrary/delete",
            data: JSON.stringify({
                data: {
                    "foodLibraryId":foodLibraryId
                }
            }),
            success: function (data) {
                if(data.success){
                    callback(data.data);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "删除食物库出错！");
                }
            }
        });
    }

	return servers;
});